var chargingStationData = [];
var parentNameData = [];

$(document).ready(function() {
	showLoading();
	$('title', parent.document).html($('title').text());
	setBaseUrl();
	$ajaxList('branchCompany', getBranchCompany);
	createTableHead("entryStatisticsThead", entryStatisticsTable);
	selectDate("startDate");
	selectDate("endDate");
	setTimeout(hideLoading, 500);
});
function getBranchCompany(data) {
	var length = data.length;
	for (var i = 0;i < length; i++){
        $("#branchCompanyID").append("<option value='" + data[i].branchCompanyID + "'>" + data[i].branchCompanyName + "</option>");
    }
}

function changeCompany() {
	parentNameData = [];
	var branchCompany = $("#branchCompanyID").val();
	if(branchCompany) {
		$ajaxList('chargingStation', getChargingStation, {BulidStatus: '3', BranchCompanyID : $("#branchCompanyID").val()});
	} else {
		$("#branchCompanyID").tips({
			side : 2,
			msg : "请选择运营公司",
			bg : '#AE81FF',
			time : 2
		});
	}
}

function getChargingStation(data) {
	var length = data.length;
	chargingStationData = data;
	var item = undefined;
	for (var i = 0; i < length; i++) {
		$("#chargingStationID").append("<option value='" + data[i].chargingStationID + "'>" + data[i].chargingStationName + "</option>");
		item = {};
		item.ID = data[i].chargingStationID;
		item.label = data[i].chargingStationName;
		item.value = data[i].chargingStationName;
		parentNameData.push(item);
	}
	$(function(){
		autoComplete("name", parentNameData, '', "chargingStationID");
		bindInput("name", onInput);  
	});
}
function onInput(){
	$("#chargingStationID").val('');
}
function getChargingStationID(){
	var value = $("#name").val();
	if (!value) {
		$("#chargingStationID").val('');
	} else {
		if (!$("#chargingStationID").val() && parentNameData && parentNameData.length > 0) {
			var matcherData = filter(parentNameData, value);
			if (matcherData) {
				var length = matcherData.length;
				for (var i=0; i<length; i++) {
					if (matcherData[i] && matcherData[i].value == value) {
						$("#chargingStationID").val(matcherData[i].ID);
						break;
					}
				}
			}
		}
	}
}

function search(){
	$ajax('searchEntryStatistics', 'entryStatisticsSearch', searchEntryStatistics);
}

function searchEntryStatistics(data){
	var entryStatisticsData = data && data.entryStatisticsList || [];
	createTableBody("entryStatisticsTbody", entryStatisticsTable, entryStatisticsData, false, true);
}